﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using CaiyinSoft.NetGather.Protocol;
using CaiyinSoft.NetGather.NetGatherLib;

namespace CaiyinSoft.NetGather.NetGather
{
    [Serializable]
    public class TaskList
    {

        public string SavePath { get; set; }
        public List<TaskListItem> Tasks;
        public TaskList(string savePath)
        {
            Tasks = new List<TaskListItem>();
            this.SavePath = savePath;
        }

        public void AddItem(TaskListItem task)
        {
            Tasks.Add(task);
        }

        public void DelItem(TaskListItem task)
        {
            Tasks.Remove(task);
        }

        public TaskListItem Find(string taskID)
        {
            return Tasks.Find( m => m.TaskID.ToLower().CompareTo(taskID) == 0);
        }

        public static void Save(string Path, TaskList taskList)
        {
            if (File.Exists(Path))
                File.Delete(Path);

            using (FileStream stream = new FileStream(Path, FileMode.Create))
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                formatter.Serialize(stream, taskList);
            }
        }

        public static TaskList Read(string Path)
        {
            if (!File.Exists(Path))
                return null;



            using (FileStream stream = new FileStream(Path, FileMode.Open))
            {
                if (stream.Length == 0)
                {
                    return new TaskList(Path);
                }
                else
                {
                    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    TaskList tl = formatter.Deserialize(stream) as TaskList;
                    tl.SavePath = Path;
                    return tl;
                }
            }
        }
    }


    [Serializable]
    public class TaskListItem
    {
        public string TaskName { get; set; }
        public string TaskID { get; set; }
        public string SavePath { get; set; }

        //public string GetLocalFullPath()
        //{
        //    if (string.IsNullOrEmpty(TaskID))
        //    {
        //        TaskID = Guid.NewGuid().ToString();
        //    }

        //    if (string.IsNullOrEmpty(TaskName))
        //    {
        //        TaskName = TaskID;
        //    }


        //    string path = Path.Combine(GloalVariable.StartUpPath, string.Format(@"tasks\{0}{1}", TaskName, Task.Extension));
        //    return path;
        //}

        //public string GetNewFileFullPath()
        //{
        //    string path = Path.Combine(GloalVariable.StartUpPath, string.Format(@"tasks\{0}{1}", TaskName, Task.Extension));
        //    return path;
        //}

        public string GetFullPath()
        {
            if (!string.IsNullOrEmpty(SavePath) && File.Exists(SavePath))
            {
                return SavePath;
            }
            else 
            {
                string path = "";
                if (!string.IsNullOrEmpty(TaskID))
                {
                    path = Path.Combine(GloalVariable.StartUpPath, string.Format(@"tasks\{0}{1}", TaskID, Task.Extension));
                    if (System.IO.File.Exists(path))
                        return path;
                }

                if (!string.IsNullOrEmpty(TaskName))
                {
                    path = Path.Combine(GloalVariable.StartUpPath, string.Format(@"tasks\{0}{1}", TaskName, Task.Extension));
                    if (System.IO.File.Exists(path))
                        return path;
                }

                path = Path.Combine(GloalVariable.StartUpPath, string.Format(@"tasks\{0}{1}", TaskName, Task.Extension));
                return path;
            }
        }
    }

}
